/*******************************************************************************
 * Copyright (C), 2014, BeeIoT Tech. Co., Ltd. 
 * FileName:      atcmd.c
 * Author:        lvxin 
 * Version :      1.0
 * Date:          2014-07-28 14:17:37
 * Description:   SIM900A通信模块
 * Function List: // 主要函数及其功能
 * 1. ------- 
 * History:
 * <author>   <time>                <version >  <desc> 
 * lvxin      2014-07-28 14:17:37   1.0         build this moudle 
 ******************************************************************************/

/*******************************************************************************
 *  *INCLUDES
 *  */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include "cssl.h"
#include "SIM900A.h"
#include "option.h"
#include "smset.h"
#include "gprs.h"
#include "at_service.h"
#include "SMS.h"
#include "LPC1114/UARTINT/USER_CODE/config.h"


/*******************************************************************************
 *  *GLOBAL VARIABLES
 *  */
cssl_t *serial = NULL;
option_t  g_sys_options; //存储全局的系统设置
int g_socket_fd = 0;

/*******************************************************************************
 *  *EXTERNAL FUNCTIONS
 *  */
void SIG_handle(int s);

/*******************************************************************************
 *  *PUBLIC FUNCTIONS
 *  */
void *serial_recv_thread(void *arg);  //串口接收线程

/*******************************************************************************
 * Function:      serial_callback()
 * Description:   串口收到数据后回调函数
 * Calls:         None
 * Called By:     cssl库
 * Input:         id      --传入ID
 *                buffer  --数据缓冲区
 *                len     --数据长度
 * Return:        void
 * Others:        None
 ******************************************************************************/
void *serial_recv_thread(void *arg) 
{
    cssl_t *serial = (cssl_t *)arg;
    int data;
    while (1)
    {
        data = cssl_getchar(serial);
        /*putchar (data);*/
        fflush(stdout);
        SIM900A_core_getch(data);
    }
}


int tty = 0;
struct termios newt, oldt;
/*******************************************************************************
 * Function:      keyboard_event_thread()
 * Description:   串口收到数据后回调函数
 * Calls:         None
 * Called By:     cssl库
 * Input:         id      --传入ID
 *                buffer  --数据缓冲区
 *                len     --数据长度
 * Return:        void
 * Others:        None
 ******************************************************************************/
void *keyboard_event_thread(void *arg) 
{
    /*char buffer[512] = {0};*/
    char ch;
    /*int i = 0;*/

    at_service_init ();

    putchar ('>');
    fflush(stdout);
    /*while (1)*/
    /*{*/
        /*read(tty, &ch, 1);*/
        /*if (ch == '\n' || i >= 510)*/
        /*{*/
            /*vsh_exec (buffer);*/
            /*i = 0;*/
            /*memset (buffer, 0, 512);*/
            /*putchar ('>');*/
            /*fflush(stdout);*/
        /*}*/
        /*else*/
        /*{*/
            /*buffer[i ++] = ch;*/
        /*}*/
    /*}*/
    

    while (1)
    {
        read(tty, &ch, 1);
        if (ch == '\n')
        {
            at_cmdline_get_char ('\r');
        }
        at_cmdline_get_char (ch);
    }
}


void alarm_demo(void)
{                  
    /*SIM900A_add_dp_alarm(9, 1, 100);*/
    SIM900A_add_dp_alarm(0, 1, 110);
    /*SIM900A_add_dp_alarm(0, 1, 120);*/
    /*SIM900A_add_dp_alarm(0, 2, 821);*/
    /*SIM900A_add_dp_alarm(0, 2, 822);*/
    /*SIM900A_add_dp_alarm(0, 2, 823);*/
    /*SIM900A_add_dp_alarm(0, 6, 824);*/
    /*SIM900A_add_dp_alarm(0, 6, 825);*/
    /*SIM900A_add_dp_alarm(0, 7, 916);*/
}

 /*查看大棚数据*/
/*******************************************************************************
 * Function:      sm_option_view_dp_info()
 * Description:   通过短信查看大棚数据
 * Calls:         
 * Called By:     sm_inner_cmd_handle_view_dp()
 * Input:         dp_num    --大棚序号
 * Return:         
 * Others:        None
 ******************************************************************************/
real_dp_d_t m_GreenHouse;



int main(int argc, char *argv[])
{
#define     DEVICE   "/dev/ttyUSB0"
    pthread_t recv_thread;
    pthread_t keyboard_event_th;
    uint8_t a[11]= {1,5,0,4,0,0,0,2,1,9,3};
    /*uint8_t a[11]= {1,8,8,4,2,3,2,7,8,3,5};*/
    tty = open("/dev/tty", O_RDONLY);
    

    cssl_start ();    //串口准备
    serial = cssl_open (DEVICE,NULL, 1, 9600, 8, 1, 1);//打开串口
    if ( NULL == serial)
    {
        perror ("open "DEVICE);
        exit (EXIT_FAILURE);
    }

    //获取终端属性
    tcgetattr(tty, &oldt);
    newt = oldt;
    //设置字符不缓冲且不回显
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr(tty, TCSANOW, &newt);

    option_get (&g_sys_options);//读取全局设置
    option_set (&g_sys_options);

    signal(SIGINT, SIG_handle);

    pthread_create (&recv_thread, NULL, serial_recv_thread, (void *)serial);//接收串口

        DBG_PRINTF ("Open SIM900A %d\n", SIM900A_open());

    if (SIM900A_open() == -1) //打开SIM900A
    {
        DBG_PRINTF ("Open SIM900A error!\n");
        exit (EXIT_FAILURE);
    }

    g_sys_options.alarm_mode = 1; //微信报警

    sm_init(&g_sys_options);
    SIM900A_init(&g_sys_options); //SIM900A初始化

    pthread_t event_th;
    pthread_create  (&event_th, NULL, SIM900A_thread_cycle_check_regs, NULL);//初始化sim卡注册状态定时检测

    pthread_create (&keyboard_event_th, NULL, keyboard_event_thread, NULL);//接收键盘

    memcpy (g_sys_options.telephone[0].number, a, 11);
    g_sys_options.dp[1].temperature_high = 300;
    g_sys_options.dp[1].temperature_low  = 200;
    g_sys_options.dp[2].temperature_high = 300;
    g_sys_options.dp[2].temperature_low  = 200;
    g_sys_options.dp[6].temperature_high = 300;
    g_sys_options.dp[6].temperature_low  = 200;
    g_sys_options.dp[7].temperature_high = 320;
    g_sys_options.dp[7].temperature_low  = 200;
    g_sys_options.number = 6;

    SMS_connect ("15040002193", 11);
    /*SMS_connect ("18842327835", 11);*/
                             
    /*view_dp(0, &g_sys_options);*/
    //SIM900A_send_power_alarm(2);
    while (1)
    {
        usleep (1000);
        SIM900A_main_loop ();
        at_service_main_loop ();
    }

    cssl_close (serial);
    cssl_stop ();
    exit (EXIT_SUCCESS);
}


void SIG_handle(int s)
{
    DBG_PRINTF ("程序终止！\n");
    cssl_close (serial);
    cssl_stop ();
    tcsetattr(tty, TCSANOW, &oldt);
    at_service_destroy ();
    exit (EXIT_SUCCESS);
}
